package com.example.bailin.giftsay.hot.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.HotEntity;
import com.example.bailin.giftsay.hot.ShouCListener;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

/**
 * Created by bailin on 16/8/17.
 * 热门适配器
 */
public class HotAdapter extends RecyclerView.Adapter<HotAdapter.HotViewHolder> {
    private HotEntity hotEntity;
    private LmClickListener lmClickListener;

    private ShouCListener cListener;

    public void setcListener(ShouCListener cListener) {
        this.cListener = cListener;
    }

    public void setLmClickListener(LmClickListener lmClickListener) {
        this.lmClickListener = lmClickListener;
    }

    public void setHotEntity(HotEntity hotEntity) {
        this.hotEntity = hotEntity;
    }

    @Override
    public HotViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_hot, parent, false);
        return new HotViewHolder(view);
    }

    @Override
    public void onBindViewHolder(HotViewHolder holder, final int position) {
        NetTool tool = new NetTool();
        tool.getImg(hotEntity.getData().getItems().get(position).getData().getCover_image_url(), holder.ivHotCoverImageUrl);
        holder.tvHotName.setText(hotEntity.getData().getItems().get(position).getData().getName());
        holder.tvHotPrice.setText("¥" + hotEntity.getData().getItems().get(position).getData().getPrice());
        holder.tvHotCount.setText("" + hotEntity.getData().getItems().get(position).getData().getFavorites_count());


        holder.rlHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cListener.onClick(hotEntity, position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return hotEntity.getData().getItems().size();
    }


    public class HotViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivHotCoverImageUrl; // 大图片
        private TextView tvHotName; // 简单介绍
        private TextView tvHotPrice;// 价格
        private TextView tvHotCount; // 收藏次数
        private RelativeLayout rlHot;

        public HotViewHolder(View itemView) {
            super(itemView);
            ivHotCoverImageUrl = (ImageView) itemView.findViewById(R.id.iv_hot_cover_image_url);
            tvHotName = (TextView) itemView.findViewById(R.id.tv_hot_name);
            tvHotPrice = (TextView) itemView.findViewById(R.id.tv_hot_price);
            tvHotCount = (TextView) itemView.findViewById(R.id.tv_hot_count);
            rlHot = (RelativeLayout) itemView.findViewById(R.id.rl_hot_id);
        }
    }
}
